﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LDAPaper
{
    public static class Extensions
    {
        public static Random rand = new Random();    

        public static double[] AddNoiseAndSilence(this double[] vector, int silenceLength, double silenceSigma, double additiveSigma)
        {
            double[] rVal = new double[vector.Length];

            for (int i = 0; i < silenceLength; i++)
                rVal[i] = GetRandomNormal(0, silenceSigma);

            Array.Copy(vector, 0, rVal, silenceLength, vector.Length - silenceLength);

            for (int i = silenceLength; i < rVal.Length; i++)
                rVal[i] = rVal[i] + ( GetRandomNormal(0, additiveSigma) - GetRandomNormal(0, additiveSigma) );

            return rVal;
        }
        
        public static double GetRandomNormal(double mean, double sigma)
        {
            double u1 = rand.NextDouble();
            double u2 = rand.NextDouble();
            double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); //random normal(0,1)
            double randNormal = mean + sigma * randStdNormal; //random normal(mean,stdDev^2)

            return randNormal;
        }

    }
}
